package com.ljf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljf.dto.DeptSaveDto;
import com.ljf.dto.QueryDept;
import com.ljf.entity.Dept;
import com.ljf.entity.Employee;
import com.ljf.exception.BusinessException;
import com.ljf.mapper.DeptMapper;
import com.ljf.service.DeptService;
import com.ljf.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
    @Autowired(required = false)
    private EmployeeService employeeService;
    @Override
    public List<Dept> listBydto(QueryDept queryDept) {
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<Dept>()
                .like(StringUtils.hasText(queryDept.getName()),Dept::getName, queryDept.getName())
                .eq(StringUtils.hasText(queryDept.getState()),Dept::getState, queryDept.getState());
        return super.list(wrapper);
    }

    /**
     * 禁用
     * @param id
     */
    @Transactional
    @Override
    public void forbiddenDept(Long id) {
        //根据部门id查出是否有人正在属于这个部门
        QueryWrapper<Employee> wrapper = new QueryWrapper<Employee>().eq(id != null,"deptId",id);
        List<Employee> list = employeeService.list(wrapper);
        if (list != null && list.size()> 0){
            throw new BusinessException("操作失败，该部门正在使用");
        }
        UpdateWrapper wrapper1 = new UpdateWrapper<>().set("state",Dept.STATE_BAN).eq("id",id);
        super.update(wrapper1);
    }

    /**
     * 新增部门
     * @param saveDto
     */
    @Override
    public void saveDept(DeptSaveDto saveDto) {
        //判断是否已经存在
        QueryWrapper<Dept> wrapper = new QueryWrapper<Dept>().eq(StringUtils.hasText(saveDto.getName()),"name",saveDto.getName());
        Dept one = super.getOne(wrapper);
        if (one != null){
            throw new BusinessException("已存在该部门");
        }
        Dept dept = new Dept();
        BeanUtils.copyProperties(saveDto,dept);
        dept.setState(Dept.STATE_NORMAL);
        super.save(dept);
    }

    @Transactional
    @Override
    public void updateDept(DeptSaveDto deptSaveDto) {
        if (deptSaveDto.getState().equals(Dept.STATE_BAN)){
            //如果是禁用操作，检查有没有人使用
            QueryWrapper<Employee> wrapper = new QueryWrapper<Employee>().eq(deptSaveDto.getId()!=null,"dept_id",deptSaveDto.getId());
            List<Employee> employeeList = employeeService.list(wrapper);
            if (employeeList!= null && employeeList.size() > 0){
                throw new BusinessException("操作失败，该部门正在使用");
            }
        }
        Dept dept = new Dept();
        BeanUtils.copyProperties(deptSaveDto,dept);
        super.updateById(dept);
    }
}
